package com.yzg.simple.netty.unpacking.unpacking_solve.codec;

import com.yzg.simple.netty.unpacking.unpacking_solve.protocol.MessageProtocol;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder;

import java.util.List;

/**
 * @author misterWei
 * @create 2020年01月07号:01点47分
 * @mailbox mynameisweiyan@gmail.com
 *
 * 针对 tcp粘包拆包 的自定义的解码器
 */
public class MessageDecode extends ReplayingDecoder<Void> {
    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        //readInt  对应发送方的 writeInt -- 都是一一对应的
        int len = in.readInt();
        byte[] content = new byte[len];
        in.readBytes(content);
        out.add(new MessageProtocol(len,content));

    }
}
